<?php
namespace app\index\controller;

class Promotion extends Base
{



    public function index()
    {
        $status = 1; //是否完成任务，默认完成
        $info = $this->user;
        $grade = $info->grade;
        $grade = $grade +1;
        //去系统推荐店铺购买单数
        $system_shop_buy_num = getSettingValue('system_shop_buy_num'.$grade);
        //金额不低于
        $system_shop_buy_price = getSettingValue('system_shop_buy_price'.$grade);
        $system_order_count = 0;
        if($system_shop_buy_num>0){
            //推荐店铺
            $paidanShop = paidan($this->uid,$info->ppuid,$info->grade);
            if($paidanShop){
                //查看是否完成任务
                $system_order_count = user_buy_from_paidan_count($paidanShop->id,$info->uid,$info->grade,$system_shop_buy_price);
                if(($system_shop_buy_num-$system_order_count)>0){
                    $isCompleteSystemOrder = 0;
                    $status = 0;
                }
                else{
                    $isCompleteSystemOrder = 1;
                }
                $this->assign('isCompleteSystemOrder',$isCompleteSystemOrder);
                $this->assign('paidanShop',$paidanShop);
            }

        }

        //去直接推荐人店铺买
        $father_shop_buy_num = getSettingValue('father_shop_buy_num'.$grade);
        //金额不低于
        $father_shop_buy_price = getSettingValue('father_shop_buy_price'.$grade);
        if($father_shop_buy_num>0){
            $fatherShop = getUserShop($info->puid);
            if(!$fatherShop){
                $fatherShop = getUserShop('system');
                //要减去推荐店铺订单数
                $father_shop_buy_num = $father_shop_buy_num+$system_order_count;
            }
            //查看是否完成任务
            $father_order_count = user_buy_from_system_count($fatherShop->id,$info->uid,$info->grade,$father_shop_buy_price);
            if(($father_shop_buy_num-$father_order_count)>0){
                $isCompleteFatherOrder = 0;
                $status = 0;
            }
            else{
                $isCompleteFatherOrder = 1;
            }
            $this->assign('isCompleteFatherOrder',$isCompleteFatherOrder);
            $this->assign('fatherShop',$fatherShop);
        }


        //培养多少个
        $team_num = getSettingValue('team_num'.$grade);
        //多少等级下级
        $grade_num = getSettingValue('grade_num'.$grade);
        $taskNum = 0;
        if($team_num){
           $taskNum = peiYangUser($info->uid,$grade_num);
           if($taskNum-$team_num>=0){
               $isCompleteTeamNum = 1;
           }
           else{
               $isCompleteTeamNum = 0;
               $status = 0;
           }
           $this->assign('isCompleteTeamNum',$isCompleteTeamNum);
        }
        $this->assign('taskNum',$taskNum);

        //需要积分
        $promotion_credit = getSettingValue('promotion_credit'.$grade);

        if($promotion_credit){
            $userCredit = $info->credit;
            if($userCredit - $promotion_credit >0){
                $isCompleteCredit = 1;
            }
            else{
                $isCompleteCredit = 0;
                $status = 0;
            }
            $this->assign('isCompleteCredit',$isCompleteCredit);
            $this->assign('userCredit',$userCredit);
        }

        //获得订单数
        $system_shop_get_order = getSettingValue('system_shop_get_order'.$grade);
        //获得客户数
        $system_shop_get_friends = getSettingValue('system_shop_get_friends'.$grade);
        //获得派单数
        $system_shop_get_paidan = getSettingValue('system_shop_get_paidan'.$grade);
        //获得积分
        $system_shop_get_credit = getSettingValue('system_shop_get_credit'.$grade);

        //是否有店铺
        $shop = getUserShop($this->uid);
        $num = getSettingValue('open_shop_num');
        if($grade>$num && !$shop){
            $status = 0;
        }

        $this->assign('num',$num);
        $this->assign('status',$status);
        $this->assign('shop',$shop);
        $this->assign('info',$info);
        $this->assign('system_shop_buy_num',$system_shop_buy_num);
        $this->assign('system_shop_buy_price',$system_shop_buy_price);
        $this->assign('father_shop_buy_price',$father_shop_buy_price);
        $this->assign('system_shop_get_order',$system_shop_get_order);
        $this->assign('father_shop_buy_num',$father_shop_buy_num);
        $this->assign('system_shop_get_friends',$system_shop_get_friends);
        $this->assign('team_num',$team_num);
        $this->assign('grade_num',$grade_num);
        $this->assign('system_shop_get_paidan',$system_shop_get_paidan);
        $this->assign('promotion_credit',$promotion_credit);
        $this->assign('system_shop_get_credit',$system_shop_get_credit);
        return view('index');
    }



    public function promotion(){

        $info = $this->user;
        $grade = $info->grade;
        $info->grade = 1+ $grade;
        $credit = getSettingValue('system_shop_get_credit'.$grade);
        $info->credit = $credit + $info->credit;
        $info->save();
        $this->redirect('index');
    }





    public function sharePic(){

        header("content-type: image/png");
           //我的分享二维码
           $shop =  $shop = getUserShop($this->uid);
           if($shop){
               $url = 'http://99.qikes.cn/public/index/shop/index.html?id='.$shop->id.'&tjr='.$this->uid;
           }
           else{
               $url = 'http://99.qikes.cn/public/index/shop/index.html?tjr='.$this->uid;
           }

           QR($url);

           die;
    }










}
